package com.jia.train.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.jia.train.global.enums.BusinessEnum;
import com.jia.train.global.exception.BusinessException;
import com.jia.train.global.utils.JwtTokenUtil;
import com.jia.train.global.utils.SnowIdUtil;
import com.jia.train.member.entity.Member;
import com.jia.train.member.entity.MemberExample;
import com.jia.train.member.mapper.MemberMapper;
import com.jia.train.member.request.MemberLoginRequest;
import com.jia.train.member.response.MemberLoginResponse;
import com.jia.train.member.service.MemberService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    public static final Logger logger = LoggerFactory.getLogger(MemberServiceImpl.class);

    @Resource
    private MemberMapper memberMapper;


    public int count() {
        return Math.toIntExact(memberMapper.countByExample(null));
    }

    @Override
    public void register(String mobile) {

        if (getMemberCount(mobile) > 0) {
            throw new BusinessException(BusinessEnum.MOBILE_EXISTS);
        }

        Member member = new Member();
//        member.setId(System.currentTimeMillis());
        member.setId(SnowIdUtil.getLongSnowNextId());// 数据中心+机器id
        // 雪花算法 1位不用 41位时间戳 10位工作机器 12位序列号 = 64位
        // 时钟回拨问题
        member.setMobile(mobile);
        memberMapper.insertSelective(member);
    }

    @Override
    public String sendCode(String mobile) {
        if (getMemberCount(mobile) == 0) {
            Member member = new Member();
            member.setId(SnowIdUtil.getLongSnowNextId());
            member.setMobile(mobile);
            memberMapper.insertSelective(member);
        }
        // 生成随机验证码
        String code = RandomUtil.randomNumbers(6);
        logger.info("生成短信验证码：{}", code);

        // TODO 保存短信记录表
        // 手机号 短信验证码 有效期 是否已使用 业务类型(注册 忘记密码) 发送时间 使用时间
        logger.info("保存短信记录");
        // TODO 对接短信通道 发送短信
        logger.info("对接短信通道");

        return "success";
    }

    private long getMemberCount(String mobile) {
        MemberExample memberExample = new MemberExample();
        MemberExample.Criteria criteria = memberExample.createCriteria();
        criteria.andMobileEqualTo(mobile);
        return memberMapper.countByExample(memberExample);
    }

    @Override
    public MemberLoginResponse login(MemberLoginRequest request) {
        String mobile = request.getMobile();
        String code = request.getCode();
        if (getMemberCount(mobile) == 0) {
            throw new BusinessException(BusinessEnum.MOBILE_NO_EXISTS);
        }

        // 校验短信验证码
        if (!StringUtils.equals(code, "106965")) {
            throw new BusinessException(BusinessEnum.MOBILE_CODE_ERROR);
        }

        MemberExample memberExample = new MemberExample();
        MemberExample.Criteria criteria = memberExample.createCriteria();
        criteria.andMobileEqualTo(mobile);
        List<Member> members = memberMapper.selectByExample(memberExample);
        Member member = members.get(0);

        MemberLoginResponse memberLoginResponse = BeanUtil.copyProperties(request, MemberLoginResponse.class);
        memberLoginResponse.setId(member.getId());
        String token = JwtTokenUtil.createToken(member.getId(), member.getMobile());
        memberLoginResponse.setToken(token);

        return memberLoginResponse;
    }


}
